<?php

namespace App\Http\Controllers\back;

use App\Model\answer;
use App\Model\book;
use App\Model\chapter;
use App\Model\talk;
use App\Model\writer;
use App\Model\writer_detail;
use App\Model\writer_new;
use App\Model\writer_take;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class WriterController extends Controller
{
    //作家信息
    public function userList()
    {
        //查询数据

//        $userList = writer::paginate(8);
        $userList = DB::table('writers')->paginate(3);
        //渲染视图
        return view('back.writeruser.userList',compact('userList'));
    }

    //添加作家
    public function add(Request $request)
    {
        if($request->isMethod('post')){
            $add = new writer();
            if(!preg_match('/[\x7f-\xff]/', $request->nickname)) {
                $request->session()->flash('addwriter','1');
                return back();
            }
            $add->nickname = $request->nickname;
            $rules=array(
                'nickname'=>'required|between:3,8',
                'tel'=>'required|digits:11',
                'email'=>'required|email|unique:readusers,email',
                'qq'=>'required|numeric',
                'newaddress'=>'required',
                'pwd'=>'required|between:6,12|confirmed',
                'icon'=>'required|image'
            );
            $mess=array(
                'nickname.required'=>'笔名不能为空',
                'nickname.between'=>'笔名要介于3到8位之间',
                'tel.required'=>'手机号不能为空',
                'tel.digits'=>'手机号必须为11位',
                'qq.required'=>'qq不能为空',
                'qq.numeric'=>'qq必须为数字',
                'email.unique'=>'邮箱已被注册',
                'email.required'=>'邮箱不能为空',
                'email.email'=>'邮箱格式不正确',
                'newaddress.required'=>'地址不能为空',
                'pwd.required'=>'密码不能为空',
                'pwd.between'=>'密码要介于6到12位之间',
                'pwd.confirmed'=>'两次密码不一致',
                'icon.required'=>'头像不能为空',
                'icon.image'=>'上传文件必须为图片'
            );
            $this->validate($request,$rules,$mess);
            $add->tel = $request->tel;
            $add->email = $request->email;
            $add->qq = $request->qq;
            $add->newaddress = $request->newaddress;
            $add->sex = $request->sex;
            $add->pwd = $request->pwd;
            $oldpassword=$request->password_confirmation;
            $add->regtime=time();
            $add->save();
            $id = $add->id;
            if($request->icon){
                $name = $id . '.jpg';
                $dir = public_path() . '\image\writer';

                $request->icon->move(public_path() . '/image/writer', $name);
                $upnew = writer::find($id);
                $upnew->icon = $name;
                $upnew->save();

            }
            return redirect('back/writer/userList');
        }
        return view('back.writeruser.useradd');
    }

    //修改作家信息
    public function Update(Request $request, $id)
    {
        if($request->isMethod('post')){
            $edit = writer::find($id);
            $rules=array(
                'nickname'=>'required|between:3,8',
                'tel'=>'required|digits:11',
                'email'=>'required|email|unique:readusers,email',
                'qq'=>'required|numeric',
                'newaddress'=>'required',
            );
            $mess=array(
                'nickname.required'=>'笔名不能为空',
                'nickname.between'=>'笔名要介于3到8位之间',
                'tel.required'=>'手机号不能为空',
                'tel.digits'=>'手机号必须为11位',
                'qq.required'=>'qq不能为空',
                'qq.numeric'=>'qq必须为数字',
                'email.unique'=>'邮箱已被注册',
                'email.required'=>'邮箱不能为空',
                'email.email'=>'邮箱格式不正确',
                'newaddress.required'=>'地址不能为空',
            );
            $this->validate($request,$rules,$mess);
            $edit->nickname = $request->nickname;
            $edit->tel = $request->tel;
            $edit->qq = $request->qq;
            $edit->email = $request->email;
            $edit->sex = $request->sex;
            $edit->newaddress = $request->newaddress;
            $edit->save();

            return redirect('back/writer/userList');

        }
        $user = writer::find($id);
        return view('back.writeruser.userUpdate',compact('user'));
    }

    //删除作家信息
    public function del($id)
    {
        $user = writer::find($id);
        $user->delete();
        return redirect('back/writer/userList');

    }

    //作家咨询
    public function talk()
    {
        $sum=DB::table('writers')->join('talks','.writers.id','talks.wid')->paginate(3);
        return view('back.writeruser.userTalk',compact('sum'));
    }

    //作家咨询删除
    public function delete($id)
    {
        $user = talk::find($id);
        $user->delete();
        return redirect('back/writer/userTalk');
    }

    //咨询回复
    public function answer(Request $request,$id)
    {
        if($request->isMethod('post')) {
            $new = talk::where('id', $id)->get();
            foreach ($new as $k) {
                $wid = $k->wid;
                $nickname = $k->nickname;
            }
            $add = new answer();
            $rules=array(
                'text'=>'required',
            );
            $mess=array('text.required'=>'回复不可为空');
            $this->validate($request,$rules,$mess);
            $add->wid = $wid;
            $add->nickname = $nickname;
            $add->tid = $id;
            $add->text = $request->text;
            $add->uptime = time();
            $add->save();
            $sm=talk::find($id);
            $sm->can=2;
            $sm->save();
            return redirect('back/writer/userTalk');
        }
        $list = talk::find($id);
        return view('back.writeruser.userAnswer',compact('list'));
    }

    public function book()
    {
//        $List = book::all();
        $List = DB::table('books')->paginate(3);
        return view('back/writeruser/book',compact('List'));
    }
    //删书籍
    public function drop($id)
    {
        $user = book::find($id);
        DB::table('bookrolls')->where('bid',$id)->delete();
        DB::table('chapters')->where('bid',$id)->delete();
        unlink(public_path().'/image/books/'.$id.'.jpg');
        $user->delete();
        return redirect('back/writer/books');
    }

    public function mybook($id)
    {
        $new = book::where('wid',$id)->paginate(3);
        foreach($new as $k){
            $writername = $k->writername;
        }
        return view('back.writeruser.myBook',compact('new','writername'));
    }

    public function clear($id)
    {
       $del = book::find($id);
       $del->delete();
       return back();
    }

    public function order($id)
    {
        $new=DB::table('books')->orderby('uptime','desc')->paginate();
        return view('back.writeruser.myBook',compact('new'));
    }

    public function orderby()
    {
        $List=DB::table('books')->orderby('uptime','desc')->paginate(3);
        return view('back.writeruser.book',compact('List'));
    }

    public function show($id)
    {
        $text = chapter::where('id',$id)->get();
        return view('back.writeruser.book_novel',compact('text'));
    }

    public function back()
    {
        return view('back.writeruser.userList');
    }
    //添加新闻
    public function push(Request $request)
    {
        if($request->isMethod('post')) {
            $add = new writer_new();
            $rules=array(
                'title'=>'required',
                'dist'=>'required'
            );
            $mess=array(
                'title.required'=>'新闻标题不可为空',
                'dist.required'=>'新闻正文不可为空'
            );
            $this->validate($request,$rules,$mess);
            $add->title = $request->title;

            $text=str_replace("\n","<br>",$request->dist);

            $add->dist = $text;
            $add->addtime = time();
            $add->save();
            return redirect('back/writer/newList');
        }
        return view('back.writeruser.publish');
    }

    public function newlist()
    {
//        $List = writer_new::all();
        $List = DB::table('writer_news')->paginate(4);
        return view('back.writeruser.newList',compact('List'));
    }

    public function delNew($id)
    {
        $del = writer_new::find($id);
        $del->delete();
        return back();
    }

    public function look($id)
    {
        $list = writer_new::where('id',$id)->get();
        return view('back.writeruser.newDist',compact('list'));
    }

    public function money()
    {
//        $mom = writer::all();
        $mom = DB::table('writers')->paginate(3);
        return view('back.writeruser.income',compact('mom'));
    }

    public function writerMoney($id)
    {
        $dollar=DB::table('writer_takes')->join('writers','.writers.id','writer_takes.wid')->where('writer_takes.wid',$id)->paginate(6);
        return view('back.writeruser.money',compact('dollar'));
    }

    public function cut($id)
    {
        $del = writer_take::find($id);
        $del->delete();
        return back();
    }

    public function details($id)
    {
        $deta = writer_detail::where('wid',$id)->get();
        return view('back.writeruser.userDetails',compact('deta'));
    }

    public function chop($id)
    {
         $cut = writer_detail::find($id);
         $cut->delete();
         return redirect('back/writer/userList');
    }
    

}
